package com.gem.netty.message;

import com.gem.entity.Card;
import com.gem.entity.Game;
import com.gem.enums.CardType;
import com.gem.enums.GemType;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.netty.entity.CardInfo;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.netty.service.card.IsExistCardService;
import com.gem.netty.service.card.IsTsCardService;
import com.gem.netty.service.common.IsCurrentUserRoundService;
import com.gem.netty.session.GroupSessionFactory;
import com.gem.pipeline.BusinessProcess;
import com.gem.pipeline.ProcessContext;
import com.gem.pipeline.ProcessController;
import com.gem.utils.R;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Xhy
 * CreateTime: 2022-10-02 14:37
 * 黄金压住卡牌
 */
public class TSMessage implements Message{


    @Override
    public void handler(ClientContent content) {
        ChannelHandlerContext ctx = content.getCtx();
        Game game = GameFactory.get(content.getRoomId());
        String roomId = game.getRoomId();
        CardInfo cardInfo = content.getContent().getCardInfo();
        cardInfo.setGems(Card.findNByNoCard(game,cardInfo));
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        // 责任链模式
        ProcessContext<ClientContent> context = new ProcessContext<>();
        context.setNeedBreak(false);
        context.setData(content);
        List<BusinessProcess> processes = new ArrayList<>();
        // 是否为当前用户阶段
        processes.add(new IsCurrentUserRoundService());
        // 是否牌库有牌
        processes.add(new IsExistCardService());
        // 是否能压卡牌
        processes.add(new IsTsCardService());
        ProcessController controller = new ProcessController();
        controller.setProcessList(processes)
                .process(context);
        if (context.getNeedBreak()){
            ctx.writeAndFlush(new TextWebSocketFrame(context.getResult().toString()));
            return;
        }
        // 压牌
        tsCard(game,cardInfo);
        // 卡牌库减少
        GameFactory.reduceGameCard(content,game);
        // 补牌
        GameFactory.getShowCard(game,content);
        // 修改状态
        setState(user);

        String userName = user.getName();

        List<Channel> channels = GroupSessionFactory.getGroupSession().getMembersChannel(content.getRoomId());
        GameFactory.notifyUsers(channels,R.ok().data("color", MessageColorType.TS_CARD.type).message(userName+ " 压住了:"+ CardType.getInfo(cardInfo.getType())
                +","+cardInfo.getScore()+" 分,卡牌buff为:"+ GemType.get(cardInfo.getCardBuff())).type(content.getType()),roomId);

    }

    private void setState(CurrentUser user)
    {
        user.setTSFlag(false);
    }
    private void tsCard(Game game, CardInfo cardInfo) {
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        List<Card> cardList = game.getShowCards().get(cardInfo.getType());
        for (Card card : cardList) {
            if (card.getNo() == cardInfo.getNo()){
                user.getTSCards().add(cardInfo);
            }
        }
    }


}
